// pages/income/index.js
const app = getApp()
const api = require('../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    end: false,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIncomeRecorde()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var scroll_page = this.data.page;
    if (!this.data.end) {
      scroll_page++;
      this.setData({
        page: scroll_page
      })
      this.getIncomeRecorde(this.data.activeIndex, scroll_page, )
    } else {
      return wx.showToast({
        title: '没有更多了',
        icon: 'none',
        duration: 1500
    })
      
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  /* 获取收益记录 */
  getIncomeRecorde: function (page) {
    var newList = this.data.list;
    var that = this;

    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      api.post('distribution/front/user/earnings/list', {
        page: page || 1,
        pageSize: 10,
        bid: app.globalData.shopId,
        shopId: app.globalData.shopId,
        userId: wx.getStorageSync('user').userId + ''
      }).then(res => {

        if (res.data.success && res.data.data.list.length) {
          res.data.data.list.map(item => {
            var record_item = {};
            record_item.counterpartyUserMobile = item.counterpartyUserMobile;
            record_item.createTime = item.createTime;
            record_item.earnings = item.earnings / 100;
            newList.push(record_item)
          })
          that.setData({
            list: newList
          })
          wx.hideLoading()
        }
        if (that.data.page * 10 >= res.data.data.total) {
          that.setData({
            end: true
          })
        }

        wx.hideLoading()

      })
    }, 500)

  }
})